package com.pb.wkflow.api.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.service.BaseService;
import com.pb.wkflow.api.entity.NextParticipantEntity;
import com.pb.wkflow.api.entity.ParticipantPostEntity;
import com.pb.wkflow.api.entity.ParticipantRoleEntity;
import com.pb.wkflow.api.entity.ParticipantUserEntity;
import com.pb.wkflow.api.mapper.ParticipantMapper;
import com.pb.wkflow.api.service.ParticipantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 苗益辉
 * @date 2022/9/22 9:38
 */
@Slf4j
@Service
public class ParticipantServiceImpl extends BaseService implements ParticipantService {

    @Autowired
    ParticipantMapper participantMapper;

    /**
     * 查询用户参与者列表
     * @param pageNum 页码
     * @param pageSize 页数
     * @param keyword 查询条件
     * @return Result
     */
    @Override
    public Result getUserList(Integer pageNum, Integer pageSize, String keyword) {
        String cropId = this.getCurrentUserInfo().getCorpId();
        if (pageNum != null && pageSize != null) {
            PageMethod.startPage(pageNum, pageSize);
        }
        List<ParticipantUserEntity> list = participantMapper.findUserList(keyword, cropId);
        return Result.ok(new PageInfo<ParticipantUserEntity>(list).getTotal(), list);
    }

    /**
     * 查询组织参与者列表
     * @return Result
     */
    @Override
    public Result getStructList() {
        String cropId = this.getCurrentUserInfo().getCorpId();
        return Result.ok(participantMapper.findStructList(cropId));
    }

    /**
     * 查询岗位参与者列表
     * @param postName 岗位名称
     * @param pageNum 页码
     * @param pageSize 页数
     * @return Result
     */
    @Override
    public Result getPostList(Integer pageNum, Integer pageSize, String postName) {
        String cropId = this.getCurrentUserInfo().getCorpId();
        if (pageNum != null && pageSize != null) {
            PageMethod.startPage(pageNum, pageSize);
        }
        List<ParticipantPostEntity> list = participantMapper.findPostList(postName, cropId);
        return Result.ok(new PageInfo<ParticipantPostEntity>(list).getTotal(), list);
    }

    /**
     * 查询角色参与者列表
     * @param pageNum 页码
     * @param pageSize 页数
     * @param roleName 角色名称
     * @return Result
     */
    @Override
    public Result getRoleList(Integer pageNum, Integer pageSize, String roleName) {
        String cropId = this.getCurrentUserInfo().getCorpId();
        if (pageNum != null && pageSize != null) {
            PageMethod.startPage(pageNum, pageSize);
        }
        List<ParticipantRoleEntity> list = participantMapper.findRoleList(roleName, cropId);
        return Result.ok(new PageInfo<ParticipantRoleEntity>(list).getTotal(), list);
    }

    /**
     * 获取下一节点参与者
     * @param procInstId 流程实例id
     * @param applyTypes 申请类型
     * @return List<ParticipantUserEntity>
     */
    @Override
    public List<NextParticipantEntity> findNextParticipant(String procInstId, List<String> applyTypes) {
        return participantMapper.findNextParticipants(procInstId, applyTypes);
    }


}
